package com.hdd.fmh.module.sys.controller;

import com.github.pagehelper.PageInfo;
import com.hdd.fmh.common.annotation.NoToken;
import com.hdd.fmh.common.response.ResultUtils;
import com.hdd.fmh.common.response.ResultVo;
import com.hdd.fmh.common.utils.StringUtils;
import com.hdd.fmh.module.sys.bean.User;
import com.hdd.fmh.module.sys.controller.dto.UserDto;
import com.hdd.fmh.module.sys.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping(value = "/user")
@RestController
public class UserController  {

    @Autowired
    UserService userService;

    @Autowired
    Redisson redisson;

    @NoToken
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultVo<User> login(String loginName, String password){
        try {
            // 1 参数校验 TODO
            if(StringUtils.isAllBlank(loginName,password))  {
                return ResultUtils.param();
            }
            ResultVo<User> user =   userService.doLogin(loginName,password);
            return user;
        }catch (Exception e){
            if(log.isDebugEnabled()){
                log.error("UserController->find->error{}" ,e);
            }
            return ResultUtils.sysError(e.getMessage());
        }
    }

    /**
     * 修改--新增
     * @param user
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResultVo<?> save(@RequestBody User user){
        // 1 参数校验 TODO
        if(StringUtils.isNullOrEmpty(user))  {
            return ResultUtils.param();
        }
        if(StringUtils.isNullOrEmpty(user.getName()))  {
            return ResultUtils.param();
        }
        RLock redissonLock = redisson.getLock(user.getName());
        try {
            // 加锁
            redissonLock.lock();
            userService.save(user);
            return ResultUtils.success();
        }catch (Exception e){
            log.error("UserController->find->error{}" ,e);
            return ResultUtils.sysError(e.getMessage());
        }finally {
            redissonLock.unlock();
        }
    }


    /**
     * 分页查询用户
     * @param dto
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ResultVo<PageInfo<User>> page(@RequestBody UserDto dto){
        try {
            return  userService.page(dto);
        }catch (Exception e){
            log.error("UserController->save->error{}" ,e);
            return ResultUtils.sysError(e.getMessage());
        }
    }

    /**
     *  修改密码
     * @param dto
     * @return
     */
    @RequestMapping(value = "/changPassWord", method = RequestMethod.POST)
    public ResultVo<?> changPassWord(String uid, String oldPass){
        try {
            return  userService.changPassWord(uid,oldPass);
        }catch (Exception e){
            log.error("UserController->save->error{}" ,e);
            return ResultUtils.sysError(e.getMessage());
        }
    }

}
